package org.cos.verification.verifier;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;

import org.cos.verification.automata.Automata;
import org.cos.verification.automata.AutomataBuilder;
import org.cos.verification.automata.AutomataParser;
import org.cos.verification.automata.EPDA;
import org.kxml2.io.KXmlParser;
import org.shellex.javame.utility.FileBrowser;
import org.shellex.javame.utility.IOWrapper;
import org.shellex.javame.utility.Screen;
import org.xmlpull.v1.XmlPullParser;

public class MainScreen extends Screen {
	public Form screen = null;
	
	private Command exitCmd;
	private Command startCmd;
	private Command ruleMgrCmd;
	private Command settingCmd;
	private Command ruleEditorCmd;

	private FileBrowser fileBrowser;
	private Command testCmd;
	
	static MainScreen instance;
	
	private AutomataBuilder ab = null;
	
	public MainScreen(){
		ab = new AutomataBuilder();
	}
	
	public static MainScreen getInstance()
	{
		if(instance == null)
			instance = new MainScreen();
		return instance;
	} 

	public void crackCommand(MIDlet midlet, Command cmd, Displayable screen) {
		if (cmd == exitCmd) {
			midlet.notifyDestroyed();
		} else if (cmd == startCmd) {
			startCheck(midlet);
		} else if (cmd == ruleMgrCmd) {
			try {
				RuleMgrScreen.getInstance().display(midlet);
			} catch (Exception e) {
				Alert alert = new Alert("Error!");
				alert.setType(AlertType.ERROR);
				alert.setString(e.getMessage());
				Display.getDisplay(midlet).setCurrent(alert);
			}			
		} else if (cmd == ruleEditorCmd){
			RuleViewerScreen.getInstance().display(midlet);
		}  else if (cmd == testCmd) {
			String model_xml = new String(IOWrapper.getInstance().read_res_UTF("/model_def.xml"));
			String policy_xml = new String(IOWrapper.getInstance().read_res_UTF("/policy_def.xml"));
			AutomataParser ap = new AutomataParser();
			
			ap.parse(model_xml);
			Automata model = ap.get_automata();
			
			ap.parse(policy_xml);
			Automata policy = ap.get_automata();
			
			model.name = "model";
			model.print();
			policy.name = "policy";
			policy.print();
			
			Automata new_am = model.merge(policy);
			new_am.print();
			if (new_am.check()){
				System.out.println("[Warning] Model conflicts with the policy");
			} else {
				System.out.println("[Warning] Model adapts to the policy");
			}
			
		}
	} 
	

	

	
	public Screen initCommand() {
		exitCmd = new Command("退出", Command.EXIT, 1);
		testCmd = new Command("Test", Command.ITEM, 1);

		startCmd = new Command("开始", Command.ITEM, 1);
		ruleMgrCmd = new Command("规则管理器", Command.ITEM, 1);
		settingCmd = new Command("设置", Command.ITEM, 1);
		ruleEditorCmd = new Command("规则编辑器", Command.ITEM, 1);

		screen.addCommand(exitCmd);
		//screen.addCommand(testCmd);
		screen.addCommand(startCmd);
		screen.addCommand(settingCmd);
		screen.addCommand(ruleMgrCmd);
		screen.addCommand(ruleEditorCmd);

		return this;
	}

	public Screen initUI(MIDlet midlet) {
		screen = new Form("CoS 安全策略校验器");
		try {
			screen.append(new ImageItem(
					"",
					Image.createImage("/logo.png"), 
					ImageItem.LAYOUT_CENTER| ImageItem.LAYOUT_NEWLINE_BEFORE, ""));
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		screen.append(new StringItem(null, 
				"欢迎使用CoS-策略校验器，CoS软件保障您的系统安全。点击`开始'菜单启动Java程序\n"));
		screen.append(new StringItem(null, 
				"统计信息：\n"));
		screen.append(new StringItem( 
				"规则总量：", String.valueOf(StatisticAnalyzer.getInstance().getRuleCount() )));	
		screen.append(new StringItem( 
				"\n上次访问：" , StatisticAnalyzer.getInstance().getLastVisitTime()));	
		screen.append(new StringItem( 
				"\n版本号：" , StatisticAnalyzer.getInstance().getVersion()));	


		return this;
	}


	public void setCommandListener(CommandListener cmdl) {
		screen.setCommandListener(cmdl);
	}

	public void display(MIDlet midlet) {
		Display.getDisplay(midlet).setCurrent(screen);
	}
    
	private void startCheck(MIDlet midlet) {
		OpenFileScreen.getInstance().display(midlet);

	}
	
}
